package com.ssm.controller;

import com.ssm.dao.EmployeeDao;
import com.ssm.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Collection;

/**
 * Author  zxy
 * Date  2022/11/18 上午 9:10
 */
@Controller
public class EmployeeController {

    /*
    * 查询所有员工信息  /employee-->get
    * 跳转到添加页面  /to/add-->get
    * 新增员工信息  /employee-->post
    * 跳转到修改页面  /employee/1-->get
    * 修改员工信息  /employee-->put
    * 删除员工信息  /employee/1-->delete
    * */

    @Autowired
    private EmployeeDao employeeDao;

    @RequestMapping(value = "/employee", method = RequestMethod.GET)
    public String getAllEmployee(Model model){
        // 查询所有员工
        Collection<Employee> allEmployee = employeeDao.getAll();
        // 将员工信息在请求域中共享
        model.addAttribute("allEmployee", allEmployee);
        return "employee_list";
    }

    @RequestMapping(value = "/employee", method = RequestMethod.POST)
    public String addEmployee(Employee employee){
        employeeDao.save(employee);
        // 重定向
        return "redirect:/employee";
    }

    @RequestMapping(value = "/employee/{id}", method = RequestMethod.GET)
    public String toUpdate(@PathVariable("id") Integer id, Model model) {
        Employee employee = employeeDao.get(id);
        model.addAttribute("employee", employee);
        return "employee_update";
    }

    @RequestMapping(value = "/employee", method = RequestMethod.PUT)
    public String updateEmployee(Employee employee) {
        employeeDao.save(employee);
        return "redirect:/employee";
    }

    @RequestMapping(value = "/employee/{id}", method = RequestMethod.DELETE)
    public String deleteEmployee(@PathVariable("id") Integer id) {
        employeeDao.delete(id);
        return "redirect:/employee";
    }

}
